package com.kfit.java8.stream;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Test6 {
	public static void main(String[] args) {
		
		//limit()
		Integer[] nums = {100,30,400,200,600,20,800};
		Integer[] newNums = Arrays.stream(nums).filter(n->n>100).limit(3).toArray(Integer[]::new);
		//结果：newNums= [400, 200, 600]
		System.out.println(Arrays.toString(newNums));
		
		//skip();
		Integer[] nums2 = {100,30,400,200,600,20,800};
		Integer[] newNums2 = Arrays.stream(nums2).skip(2).toArray(Integer[]::new);
		//结果：newNums= [400, 200, 600, 20, 800]
		System.out.println(Arrays.toString(newNums2));
		
		
		//map();
		Integer[] nums3 = {2,3,5,6,7,9,8};
		Integer[] newNums3 = Arrays.stream(nums3).map(n -> n*n).toArray(Integer[]::new);
		//结果：newNums= [4, 9, 25, 36, 49, 81, 64]
		System.out.println(Arrays.toString(newNums3));
		
		
		 /**获取单词，并且去重**/
        List<String> list = Arrays.asList("hello welcome", "world hello", "hello world",
            "hello world welcome");
 
        //map和flatmap的区别
        list.stream().map(item -> Arrays.stream(item.split(" "))).distinct().collect(Collectors.toList()).forEach(System.out::println);
        System.out.println("---------- ");
        list.stream().flatMap(item -> Arrays.stream(item.split(" "))).distinct().collect(Collectors.toList()).forEach(System.out::println);
 
        //实际上返回的类似是不同的
        List<Stream<String>> listResult = list.stream().map(item -> Arrays.stream(item.split(" "))).distinct().collect(Collectors.toList());
        List<String> listResult2 = list.stream().flatMap(item -> Arrays.stream(item.split(" "))).distinct().collect(Collectors.toList());
        
		
	}
	
	
}
